package com.myblog.demo.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回对象
 */
@Data
public class AjaxResult implements Serializable {
    private int code;
    private String msg;
    private Object data;

    /**
     * 返回成功数据
     *
     * @param data 数相关据
     * @return 返回 AjaxResult 对象
     */
    public static AjaxResult success(Object data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(200);
        ajaxResult.setMsg("");
        ajaxResult.setData(data);
        return ajaxResult;
    }

    /**
     * 返回成功数据
     *
     * @param data 相关数据
     * @param msg  相关描述信息
     * @return 返回 AjaxResult 对象
     */
    public static AjaxResult success(Object data, String msg) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(200);
        ajaxResult.setMsg(msg);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    /**
     * 返回失败数据
     *
     * @param code 错误码
     * @param msg  错误信息
     * @return 返回 AjaxResult 对象
     */
    public static AjaxResult fail(Integer code, String msg) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(code);
        ajaxResult.setMsg(msg);
        ajaxResult.setData("");
        return ajaxResult;
    }

    /**
     * 返回失败数据
     *
     * @param code 错误码
     * @param data 相关错误数据
     * @param msg  错误信息
     * @return 返回 AjaxResult 对象
     */
    public static AjaxResult fail(Integer code, Object data, String msg) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(code);
        ajaxResult.setMsg(msg);
        ajaxResult.setData(data);
        return ajaxResult;
    }

}
